/**
 * Copyright 2009 Emory University
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated  documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the 
 * Software is furnished to do so, subject to the following conditions: The 
 * above copyright notice and this permission notice shall be included in all 
 * copies or substantial portions of the Software. 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
package com.markgrand.structuredSql;

/**
 * Append an Update statement to a StringBuilder.
 * 
 * @param tableName
 *      The name of the table to be updated. The name may be a qualified name.
 * @param
 *      An alias to use for the table name elsewhere in the update.
 * @param
 *      The names of the columns to be updated.
 * @param
 *      A where constraint to constrain which rows are updated by the update statement
 * @author Mark Grand
 */
class Update protected ( val tableName:TableName,
                         val alias:Option[String],
                         val columnNames:Seq[String],
                         val values:UpdateValueList,
                         val where:Option[WhereConstraint]) extends SQLStatement with SQLIdentifier {
  if (tableName==null) {
    throw new NullPointerException("Table name cannot be null")
  }
  if (columnNames==null) {
    throw new NullPointerException("Column names cannot be null")
  }
  if (columnNames.length==0) {
    throw new IllegalArgumentException("Length of column names array must be greater than zero.")
  }
  if (values==null) {
    throw new NullPointerException("Values cannot be null")
  }
  
  def append(builder:StringBuilder) {
    builder.append("UPDATE ")
    appendUpdateBody(builder)
  }
  
  protected def appendUpdateBody(builder:StringBuilder) {
    builder.append(tableName)
    if (alias.isDefined) {
      builder.append(" AS ")
      builder.append(alias.get)
    }
    builder.append(" SET (")
    val columnIterator = columnNames.iterator
    appendIdentifier(columnIterator.next, builder)
    while (columnIterator.hasNext) {
      builder.append(",")
      appendIdentifier(columnIterator.next, builder)
    }
    builder.append(") = ")
    values.append(builder)
    if (where.isDefined) {
      builder.append(" ")
      where.get.append(builder)
    }
  }
}

object Update {
  def apply(tableName:TableName, columnNames:Seq[String], values:UpdateValueList) = {
    new Update(tableName, None, columnNames, values, None)
  }
  def apply(tableName:TableName, alias:String, columnNames:Seq[String], values:UpdateValueList) = {
    new Update(tableName, Some(alias), columnNames, values, None)
  }
  def apply(tableName:TableName, columnNames:Seq[String], values:UpdateValueList, where:WhereConstraint) = {
    new Update(tableName, None, columnNames, values, Some(where))
  }
  def apply(tableName:TableName, alias:String, columnNames:Seq[String], values:UpdateValueList, where:WhereConstraint) = {
    new Update(tableName, Some(alias), columnNames, values, Some(where))
  }
  def apply(tableName:TableName, alias:Option[String], columnNames:Seq[String], values:UpdateValueList, where:Option[WhereConstraint]) = {
    new Update(tableName, alias, columnNames, values, where)
  }
}
